package nameless.aistudy.domain.service.subjects.english.reading.impl;

import lombok.extern.slf4j.Slf4j;
import nameless.aistudy.common.util.ai.AIUtil;
import nameless.aistudy.domain.service.subjects.english.reading.EnglishTranslateAIService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.util.Arrays;

@Service
@Slf4j
public class EnglishTranslateAIServiceImpl implements EnglishTranslateAIService {

    private final String ENGLISH_TO_CHINESE_SYSTEM_PROMPT = "翻译英文为中文，仅回复译文部分。";
    private final int ENGLISH_TO_CHINESE_MAX_TOKENS = 2048;
    private final double ENGLISH_TO_CHINESE_TEMPERATURE = 1.0;

    private final String CHINESE_TO_ENGLISH_SYSTEM_PROMPT = "翻译中文为英文，仅回复译文部分。";
    private final int CHINESE_TO_ENGLISH_MAX_TOKENS = 2048;
    private final double CHINESE_TO_ENGLISH_TEMPERATURE = 1.0;

    @Autowired
    AIUtil aiUtil;

    @Override
    public String translateEnglishToChinese(String english, String[] requirements) {
        return aiUtil.buildChatClient(ENGLISH_TO_CHINESE_MAX_TOKENS, ENGLISH_TO_CHINESE_TEMPERATURE)
                .prompt()
                .system(ENGLISH_TO_CHINESE_SYSTEM_PROMPT + "要求：" + Arrays.toString(requirements))
                .user(english)
                .call()
                .content();
    }

    @Override
    public String translateChineseToEnglish(String chinese, String[] requirements) {
        return aiUtil.buildChatClient(CHINESE_TO_ENGLISH_MAX_TOKENS, CHINESE_TO_ENGLISH_TEMPERATURE)
                .prompt()
                .system(CHINESE_TO_ENGLISH_SYSTEM_PROMPT + "要求：" + Arrays.toString(requirements))
                .user(chinese)
                .call()
                .content();
    }

    @Override
    public SseEmitter translateEnglishToChineseStream(String english, String[] requirements) {
        String systemPrompt = ENGLISH_TO_CHINESE_SYSTEM_PROMPT + "要求：" + Arrays.toString(requirements);
        return aiUtil.buildSseEmitter(systemPrompt, english);
    }

    @Override
    public SseEmitter translateChineseToEnglishStream(String chinese, String[] requirements) {
        String systemPrompt = CHINESE_TO_ENGLISH_SYSTEM_PROMPT + "要求：" + Arrays.toString(requirements);
        return aiUtil.buildSseEmitter(systemPrompt, chinese);
    }
}
